Your goal is to create this plot using plotly.

  1. Download https://michaelgastner.com/DAVisR_data/life_quality.csv.

  2. Make a scatterplot where:

lq |>
  plot_ly(
    x = ~gdp_per_capita,
    y = ~life_expectancy,
    mode = "markers",
    type = "scatter")
  1. Set the colors of each point to indicate which continent it belongs to.
lq |>
  plot_ly(
    x = ~gdp_per_capita,
    y = ~life_expectancy,
    mode = "markers",
    type = "scatter",
    color = ~continent,
    colors = colors) 
  1. Set the size of each bubble to indicate the population.
lq |>
  plot_ly(
    x = ~gdp_per_capita,
    y = ~life_expectancy,
    mode = "markers",
    type = "scatter",
    color = ~continent,
    colors = colors,
    fill = ~"",
    size = ~pop,
    # adjusts the range of sizes used 
    marker = list(
      sizemode = "diameter",
      opacity = 0.6,
      sizes = c(10, 50)) # choosing the range of the bubble sizes
  )
  1. Assign texts to each point that will show when you hover over it.
lq |>
  plot_ly(
    x = ~gdp_per_capita,
    y = ~life_expectancy,
    mode = "markers",
    type = "scatter",
    color = ~continent,
    colors = colors,
    fill = ~"",
    size = ~pop,
    marker = list(
      sizemode = "diameter",
      opacity = 0.6,
      sizes = c(10, 50) 
    ),
    hoverinfo = "text",
    # placing a dollar sign in front of GDP per Capita 
    text = ~ paste(
      "Country:", country_name,
      "<br>GDP per capita:", paste0("$", round(gdp_per_capita, digit = 2)),
      # round off GDP per Capita and life expectancy to 2 digits 
      "<br>Life Expectancy:", paste(round(life_expectancy, digits = 2), "years"),
      "<br>Population:", pop
    )
  )
  1. Add labels to the plot using layout()
lq |>
  plot_ly(
    x = ~gdp_per_capita,
    y = ~life_expectancy,
    mode = "markers",
    type = "scatter",
    color = ~continent,
    colors = colors,
    fill = ~"",
    size = ~pop,
    marker = list(
      sizemode = "diameter",
      opacity = 0.6,
      sizes = c(10, 50), 
      #  Add a thin white border around the points to make it easier to distinguish overlapping points
      line = list(
        width = 1,
        color = "white"
      )
    ),
    hoverinfo = "text",
    text = ~ paste(
      "Country:", country_name,
      "<br>GDP per capita:", paste0("$", round(gdp_per_capita, digit = 2)),
      "<br>Life Expectancy:", paste(round(life_expectancy, digits = 2), "years"),
      "<br>Population:", pop
    )
  ) |>
  layout(
    title = "Wealth and Health by Country in 2015",
    # making sure the sizes of the symbol in the legend are constant.
    legend = list(
      title = list(text = "<b> Continent <b>"),
      itemsizing = "constant"
    ),
    xaxis = list(
      title = "GDP per capita (USD, PPP2015)",
      type = "log",
      showgrid = TRUE,
      tickmode = "linear"
    ),
    yaxis = list(
      title = "Life Expectancy (years)"
    ),
    annotations = list(
      x = 1.2, y = -0.1,
      text = "Source: World Bank",
      showarrow = F, xref = "paper", yref = "paper",
      xanchor = "right", yanchor = "auto", xshift = 0, yshift = 0,
      font = list(size = 12)
    ) 
  )     
  1. Fine-tune and complete the plot.

Thank you!